﻿using System;
using System.Linq;
using CourseManager.Model.Data;
using CourseManager.Model.Util;
using Ext.Net;

namespace CourseManager.View.Contents.Admin
{
	public partial class StudentManageContent : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				DBEntity ent = new DBEntity();
				var source = from student in ent.Tab_Student
							 from user in ent.Tab_User
							 where student.studentId.Equals(user.userId)
				             select new
				                    	{
											studentId = student.studentId,
											name = student.name,
											sex = student.sex,
											dept = student.dept,
											grade = student.grade,
											password = user.password
				                    	};
				StudentStore.DataSource = source;
				StudentStore.DataBind();
			}
		}


		[DirectMethod]
		public void ShowUpdateWindow(string id, string name, int grade, string sex, string dept)
		{
			txtUpdateId.Text = id;
			txtUpdateName.Text = name;
			txtUpdateDept.Text = dept;
			cbUpdateGrade.Text = grade.ToString();
			cbUpdateSex.Text = sex;
			UpdateWindow.Show();
		}

		[DirectMethod]
		public void DeleteStudent(string id)
		{
			StudentDAO.Remove(id);
			Response.Redirect(Request.RawUrl);
		}

		[DirectMethod]
		public void DoResetPassword(string id)
		{
			UserDAO.ResetPassword(id);
		}

		protected void BtnAddOkClick(object sender, DirectEventArgs e)
		{
			try
			{
				StudentDAO.Add(txtAddId.Text, txtAddName.Text, cbAddSex.Text,
					txtAddDept.Text, int.Parse(cbAddGrade.Text));
				Response.Redirect(Request.RawUrl);
			}
			catch (Exception ex)
			{
				ExtNet.Msg.Alert("错误", "添加失败，可能原因为ID重复");
			}
		}

		protected void BtnUpdateOkClick(object sender, DirectEventArgs e)
		{
			var ent = new DBEntity();
			var student = (from s in ent.Tab_Student
						   where s.studentId.Equals(txtUpdateId.Text)
						   select s).FirstOrDefault();
			student.name = txtUpdateName.Text;
			student.sex = cbUpdateSex.Text;
			student.grade = int.Parse(cbUpdateGrade.Text);
			
			student.dept = txtUpdateDept.Text;


//			ent.UpdateStudent(txtUpdateId.Text, txtUpdateName.Text, int.Parse(cbUpdateGrade.Text), cbUpdateSex.Text,
//			                  txtUpdateDept.Text);
			ent.SaveChanges();
			Response.Redirect(Request.RawUrl);

		}

		protected void BtnSearchClick(object sender, DirectEventArgs e)
		{
			var keyword = txtKeyword.Text.Trim();
			if (keyword.Length == 0)
			{
				DBEntity ent = new DBEntity();
				var source = from student in ent.Tab_Student
							 from user in ent.Tab_User
							 where student.studentId.Equals(user.userId)
							 select new
							 {
								 studentId = student.studentId,
								 name = student.name,
								 sex = student.sex,
								 grade = student.grade,
								 dept = student.dept,
								 password = user.password
							 };
				StudentStore.DataSource = source;
			}
			else
			{
				DBEntity ent = new DBEntity();
				var source = from student in ent.Tab_Student
				             from user in ent.Tab_User
				             where student.studentId.Equals(user.userId)
				                   && (student.studentId.Contains(keyword)
				                       || student.name.Contains(keyword)
				                       || student.sex.Equals(keyword)
				                       || student.dept.Contains(keyword))
				             select new
				                    	{
											studentId = student.studentId,
											name = student.name,
											sex = student.sex,
											grade = student.grade,
											dept = student.dept,
											password = user.password
				                    	};
				StudentStore.DataSource = source;
			}
			pagingBar.SetPageIndex(1);
			StudentStore.DataBind();
		}

	}
}